/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flarebit.flarebarlib;

import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.BlendMode;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.IOException;
import java.io.InputStream;

/**
 * Utils
 *
 * @since 2021-06-17
 */
public class FlareUtils {
    private static final String TAG = "FlareUtils";

    private static final int DEFAULT_X = 0xFF;

    private static final int DEFAULT_VALUE = 2;

    private static final int DEFAULT_8 = 8;

    private static final int DEFAULT_16 = 16;

    private static final int DEFAULT_24 = 16;

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.ERROR, 0x001, TAG);

    private FlareUtils() {
    }

    /**
     * Blend between two ARGB colors using the given ratio.
     *
     * <p>A blend ratio of 0.0 will result in {@code color1}, 0.5 will give an even blend,
     * 1.0 will result in {@code color2}.</p>
     *
     * @param color1 the first ARGB color
     * @param color2 the second ARGB color
     * @param ratio the blend ratio of {@code color1} to {@code color2}
     * @return blendArgb value
     */
    public static int blendArgb(int color1, int color2, float ratio) {
        final float inverseRatio = 1 - ratio;
        float alpha = Color.alpha(color1) * inverseRatio + Color.alpha(color2) * ratio;
        float red = red(color1) * inverseRatio + red(color2) * ratio;
        float green = green(color1) * inverseRatio + green(color2) * ratio;
        float blue = blue(color1) * inverseRatio + blue(color2) * ratio;
        return Color.argb((int) alpha, (int) red, (int) green, (int) blue);
    }

    /**
     * Return the alpha component of color int. This is the same as saying color >>> 24
     *
     * @param color color
     * @return alpha value
     */

    public static int alpha(int color) {
        return color >>> DEFAULT_24;
    }

    /**
     * Return the red component of color int. This is the same as saying (color >> 16) & 0xFF
     *
     * @param color color
     * @return red value
     */
    public static int red(int color) {
        return (color >> DEFAULT_16) & DEFAULT_X;
    }

    /**
     * Return the green component of color int. This is the same as saying (color >> 8) & 0xFF
     *
     * @param color color
     * @return green value
     */

    public static int green(int color) {
        return (color >> DEFAULT_8) & DEFAULT_X;
    }

    /**
     * Return the blue component of color int. This is the same as saying color & 0xFF
     *
     * @param color color
     * @return blue value
     */
    public static int blue(int color) {
        return color & DEFAULT_X;
    }

    /**
     * setTintElement
     *
     * @param element element
     * @param intColor intColor
     * @param mode blendMode
     * @return ShapeElement
     */
    public static ShapeElement getTintElement(ShapeElement element, int intColor, BlendMode mode) {
        int[] colors = new int[]{intColor, intColor};
        int[][] states = new int[DEFAULT_VALUE][];
        states[0] = new int[]{0};
        states[1] = new int[]{0};
        element.setStateColorList(states, colors);
        element.setStateColorMode(mode);
        return element;
    }

    /**
     * 根据资源文件获取位图
     *
     * @param context 上下文
     * @param resId 资源Id
     * @return 位图
     */
    public static PixelMap getPixelMapFromResource(Context context, int resId) {
        InputStream drawableInputStream = null;
        PixelMap pixelmap = null;
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        try {
            drawableInputStream = context.getResourceManager().getResource(resId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            pixelmap = imageSource.createPixelmap(decodingOptions);
            return pixelmap;
        } catch (IOException | NotExistException e) {
            HiLog.error(LABEL_LOG,"", "IOException");
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException e) {
                HiLog.error(LABEL_LOG,"", "IOException");
            }
        }
        return pixelmap;
    }
}
